package com.cloudkinto.service.event.stock;

import cn.hutool.json.JSONUtil;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.dto.order.outbound.SysStockDeductDto;
import com.cloudkinto.entity.OutboundPickingDo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * @author long
 * @Description 库存事件监听
 * @Date 2025/6/05 09:18
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class StockEventListener {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 订单出库时库存扣减
     *
     * @param event 事件实体
     */
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sysStockDeduct(SysStockDeductEvent event) {
//        log.error("获取单号自动发货，库存扣减事件开始");
        for (SysStockDeductDto sysStockDeductDto : event.getSysStockDeductDtoList()) {
            rabbitTemplate.convertAndSend(RabbitQueueConstant.Sys_Stock_Deduct, JSONUtil.toJsonStr(sysStockDeductDto));
        }

    }

}